<?php

class UserIdentity extends CUserIdentity
{
	private $_id;
	private $_alias;
	
	const ERROR_STATUS_INTACTIV = 201;
	const ERROR_STATUS_PENDING  = 202;
	const ERROR_STATUS_BANNED   = 203;
	
	public function authenticate($duration = 0)
	{
		$user = User::model()->notsafe()->findByAttributes(array('email' => $this->username));
		
		if($user === null)
		{
			$this->errorCode = self::ERROR_UNKNOWN_IDENTITY;
		}
		else if(!$user->checkPassword($this->password))
		{
			$this->errorCode = self::ERROR_UNKNOWN_IDENTITY;
		}
		else if($user->status == User::STATUS_INACTIVE)
		{
			$this->errorCode = self::ERROR_STATUS_INTACTIV;
		}
		else if($user->status == User::STATUS_PENDING)
		{
			$this->errorCode = self::ERROR_STATUS_PENDING;
		}
		else if($user->status == User::STATUS_BANNED)
		{
			$this->errorCode = self::ERROR_STATUS_BANNED;
		}
		else
		{
			$this->_id       = $user->id;
			$this->username  = $user->email;
			$this->setState('alias', $user->alias);
			$this->errorCode = self::ERROR_NONE;
			
			return Yii::app()->user->login($this, $duration);
		}
		return false;
	}
    
	public function getId()
	{
		return $this->_id;
	}
	
	public function getAlias()
	{
		return $this->_alias;
	}
	
}
